define(['moment'], function (moment) {
    return ['$rootScope', '$scope', '$filter', '$stateParams', 'config', 'MMWService', '$timeout',
        function ($rootScope, $scope, $filter, $stateParams, config, $MMWService, $timeout) {
            $scope.init = function(){
                $scope.button_type='';
                $scope.condition = {
                    plot_no:'',
                    serial_number_start:'',
                    serial_number_end:'',
                    serial_type:"PCSNO", //顯示序號或板號
                    pcs_on_per_panel:1 ,//預設連板號為1
                    lot_no_num:1 //預設序號數量為1
                };
                $scope.pcs_detail = [];
            };
            
            $scope.back = function(){
                $scope.$root.changeProgram('home', true);
            };

            $scope.loadPlotList = function(){
                $MMWService.showQueryListPNIG({
                    page_mode: 29,
                    pageRecord:50,
                    condition: [
                        {name:$filter('translate')('common.queryList.plot_no'), column_name:'lotno', query_mode:'2', value:$scope.condition.plot_no, merge_condition_model:'AND'}
                    ],
                    header: [
                        {label:$filter('translate')('common.queryList.plot_no'), code:'page_no'},
                        {label:$filter('translate')('common.queryList.wo_c'), code:'wono'},
                        {label:$filter('translate')('common.queryList.item_c'), code:'productno'},
                        {label:$filter('translate')('common.queryList.item_n'), code:'productname'},
                        {label:$filter('translate')('common.queryList.item_spec'), code:'itemspec'}
                    ],
                    setCondition:function(conditions){
                        conditions.push({column_name:'status', query_mode:$scope.condition.serial_type==='PCSNO'?1:0, value:'11', merge_condition_model:'AND' });
                        return conditions;
                    },
                    selectCode:$scope.condition.plot_no,
                    confirm : function(selectItem){
                        $scope.condition.plot_no = selectItem.page_no;
                    }
                });
            };

            $scope.search = function(){
                $scope.button_type='old';//給後端認知送出的是查詢後的資料
                var msg = checkCondtions();
                if(msg === ''){
                    serial_number_reprint_info_get($scope.condition);
                } else {
                    $scope.$root.showAlert(msg);
                };
            };

            // 生成新號
            $scope.generate_new_code = function(){
                $scope.button_type='new';//給後端認知送出的是新生成序號後的資料
                var msg = checkCondtions();

                if(msg === ''){
                    serial_number_generate_new_code($scope.condition);
                } else {
                    $scope.$root.showAlert(msg);
                };
            }

            function serial_number_generate_new_code(condition){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.serial_number_generate_new_code',
                    content : {
                        plot_no :condition.plot_no,
                        serial_number_start : condition.serial_number_start,
                        serial_number_end : condition.serial_number_end,
                        lot_no_num:condition.lot_no_num,//序號數量
                        pcs_on_per_panel: condition.pcs_on_per_panel, //連板號
                        serial_type: condition.serial_type
                    },
                    success : function(data){
                        $scope.pcs_detail = data.pcs_detail;
                        panel_max_detail = data.panel_max_detail;
                        create_max_detail = data.create_max_detail;
                        panel_max_detail = data.panel_max_detail;
                    }
                });
            }

            $scope.selectAll = function(){
                $scope.$root.showLoading();
                $scope.isSelectAll = !$scope.isSelectAll;
                if($scope.pcs_detail && $scope.pcs_detail.length>0){
                    $scope.pcs_detail.forEach(function(pcs){
                        pcs.isSelect = $scope.isSelectAll+'';
                    });
                }
                $scope.$root.hideLoading();
            };

            $scope.itemClick = function(item){
                item.isSelect = (item.isSelect == 'true'?'false':'true');
                if(item.isSelect=='false'){
                    $scope.isSelectAll = false;
                }
            };

            $scope.confirm = function(){
                if($scope.button_type==="old"){
                    serial_number_process_json($scope.condition, function(data){//補印流程
                        //data
                        $scope.$root.showToast($filter('translate')('SMT19.msg.success'));
                        printTag(data.pcs_detail, 0);
                    });
                }else if($scope.button_type==="new"){//新增序號流程
                     serial_number_save_json($scope.condition,function(){
                        $rootScope.showConfirm($filter('translate')('SMT19.msg.save_success'),function(dialog){
                            serial_number_process_json($scope.condition,function(data){
                                $scope.$root.showToast($filter('translate')('SMT19.msg.success'));
                                printTag(data.pcs_detail, 0);
                            });
                            dialog.hide();
                        });
                    })
                }
            };

            $scope.switch_serial_type=function(type){
                $scope.pcs_detail=[];
                $scope.condition.serial_type=type;
            }

            function checkCondtions(){
                var msg = '';
                if($scope.condition.plot_no == ''){
                    msg += $filter('translate')('SMT19.msg.plot_no_empty');
                } else if(($scope.condition.serial_number_start+$scope.condition.serial_number_end) != ''){
                    if($scope.condition.serial_number_start == '' || $scope.condition.serial_number_end.length == ''){
                        msg += $filter('translate')('SMT19.msg.serial_no_not_enough');    //起始序號未填寫完整
                    }else if(isNaN($scope.condition.serial_number_start) || isNaN($scope.condition.serial_number_end)){
                        msg += $filter('translate')('SMT19.msg.serial_no_illegal');   //成品序號格式錯誤
                    }else if(+$scope.condition.serial_number_start > +$scope.condition.serial_number_end){
                        msg += $filter('translate')('SMT19.msg.serial_no_start_less_than_end'); //起始序號不可大於結束序號
                    }
                } else if($scope.condition.serial_type==='PANELNO' && $scope.condition.pcs_on_per_panel===''){
                    msg +=  $filter('translate')('SMT19.msg.choose_panel_num'); //請輸入panel序號組數
                }
                return msg;
            };

            var panel_max_detail = [];
            var create_max_detail = [];
            // 顯示舊號：補印
            function serial_number_reprint_info_get(condition){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.serial_number_reprint_info_get',
                    content : {
                        plot_no :condition.plot_no,
                        serial_number_start : condition.serial_number_start,
                        serial_number_end : condition.serial_number_end,
                        serial_type:condition.serial_type,//序號或板號
                    },
                    success : function(data){
                        $scope.pcs_detail = data.pcs_detail;
                        create_max_detail = data.create_max_detail;
                        panel_max_detail = data.panel_max_detail;
                        panel_max_detail = data.panel_max_detail;
                    }
                });
            };

            //確定：序號列印
            function serial_number_process_json(condition, callback){
                $MMWService.sendToServer({
                uri : 'ServicesSMT.Module_SMT.serial_number_print_process',
                    content : {
                        ip: config.server.ip,
                        port: config.server.port,
                        plot_no :condition.plot_no,
                        serial_type:condition.serial_type,
                        pcs_detail:$scope.pcs_detail,
                        create_max_detail:create_max_detail,
                        panel_max_detail:panel_max_detail
                    },
                    success : function(data){
                        //$scope.pcs_detail = data.pcs_detail;
                        if(callback)
                            callback(data);
                    }
                });
            };

            //確定：存檔
            function serial_number_save_json(condition,callback){
                $MMWService.sendToServer({
                uri : 'ServicesSMT.Module_SMT.serial_number_save_process',
                    content : {
                        ip: config.server.ip,
                        port: config.server.port,
                        plot_no :condition.plot_no,
                        pcs_detail:$scope.pcs_detail,
                        create_max_detail:create_max_detail,
                        panel_max_detail:panel_max_detail,
                        serial_type:condition.serial_type,
                        button_type:$scope.button_type
                    },
                    success : function(data){
                        //$scope.pcs_detail = data.pcs_detail;
                        if(callback)
                            callback(data);
                    }
                });
            }

            function printTag(tags, index){
                if(tags && index < tags.length){
                    if(window.nodeRequire){
                        var exec = window.nodeRequire('child_process').exec;
                        $rootScope.showToast($filter('translate')('SMT19.msg.prpare_print', {index:index}));
                        exec(tags[index].commandline, function(error, stdout, stderr){
                            if(error){
                                $rootScope.showAlert($filter('translate')('E01.msg.cantprint'));
                            }
                            index = index +1;
                            printTag(tags, index);
                        });
                    }
                }
            };

        }
    ];
});
